<template>
	<div class="invaStyle">
		<el-header>
			<el-button
				:disabled="searchLoading"
				type="primary"
				icon="el-icon-circle-plus-outline"
				size="mini"
				v-permissionKey="['add_team']"
				@click="formCom('新增')"
				>新增</el-button
			>
			<el-form>
				<el-form-item label="基金代码/名称：">
					<el-select
						filterable
						:disabled="searchLoading"
						clearable
						v-model="fundManagerQuery.fundSysCode"
						@change="statuCheck(1)"
						placeholder="请选择基金"
						size="medium"
					>
						<el-option
							v-for="(item, index) in fundList"
							:key="index + item.fundSysCode"
							:label="item.fundCode + '-' + item.fundFullName"
							:value="item.fundSysCode"
						>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="投资团队：">
					<el-select :disabled="searchLoading" clearable filterable v-model="fundManagerQuery.invest" @change="statuCheck(1)" size="medium">
						<el-option key="" label="--请选择--" value=""></el-option>
						<el-option v-for="item in productDictList" :key="item.typeCode + 'a'" :label="item.typeName" :value="item.typeCode">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label=" 状态：">
					<el-select
						:disabled="searchLoading"
						clearable
						filterable
						v-model="fundManagerQuery.checkState"
						@change="statuCheck(1)"
						size="medium"
					>
						<el-option v-for="(item, index) in checkList" :key="item.key + index" :label="item.lable" :value="item.key"></el-option>
					</el-select>
				</el-form-item>
			</el-form>
		</el-header>
		<el-main>
			<el-table
				:data="fundManagerList"
				:default-sort="{ prop: 'date', order: 'descending' }"
				:header-cell-style="{ background: '#FAFAFA' }"
				border
				v-loading="searchLoading"
				element-loading-text="正在加载数据，请耐心等待......"
				element-loading-spinner="el-icon-loading"
				element-loading-background="rgba(0, 0, 0, 0.8)"
			>
				<el-table-column label="基金名称" prop="fundSysCode" :formatter="tabFormatter"> </el-table-column>
				<el-table-column label="投资团队" prop="nvestName" width="160"> </el-table-column>
				<el-table-column label="开始时间" sortable prop="startDate" width="160">
					<template slot-scope="scope">
						{{ scope.row.startDate | timerConversionStr }}
					</template>
				</el-table-column>
				<el-table-column label="结束时间" sortable prop="endDate" width="160">
					<template slot-scope="scope">
						{{ scope.row.endDate | timerConversionStr }}
					</template>
				</el-table-column>
				<el-table-column label="状态" prop="checkState" :formatter="formatterCheckStatus" width="160"> </el-table-column>
				<el-table-column label="附件" prop="fileName" width="160">
					<template slot-scope="scope">
						<el-popover trigger="hover" placement="bottom">
							<span>{{ scope.row.fileName }}</span>
							<div slot="reference" class="ellipsis" @click="openFileDetails(scope.row)">
								<span v-if="scope.row.fileName != null && scope.row.fileName.length > 8">{{
									scope.row.fileName.substr(0, 7) + '.'
								}}</span>
								<span v-else>{{ scope.row.fileName }}</span>
							</div>
						</el-popover>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" width="280"
					>>
					<template slot-scope="scope">
						<el-button
							type="primary"
							size="mini"
							v-permissionKey="['edit_team']"
							icon="el-icon-edit"
							v-if="opsList.indexOf('f_invest_style_edit') != -1"
							@click="formCom('编辑', scope.row)"
							>编辑</el-button
						>
						<el-button
							type="primary"
							size="mini"
							v-permissionKey="['check_team']"
							v-if="scope.row.checkState === '0' && opsList.indexOf('f_assess_met_check') != -1"
							@click="formCom('复核', scope.row)"
							icon="el-icon-s-check"
							>复核</el-button
						>
						<el-button
							size="mini"
							v-permissionKey="['del_team']"
							type="danger"
							class="delebtn"
							v-if="opsList.indexOf('f_assess_met_del') != -1"
							@click="delFundAssessment(scope.row)"
							icon="el-icon-delete"
							>删除</el-button
						>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				background
				:page-size="paging2.size"
				@current-change="handleCurrentChange2"
				@size-change="handleSizeChange2"
				:current-page="paging2.num"
				:page-sizes="paging2.sizes"
				layout="sizes,prev,pager,next,jumper,total"
				:total="paging2.total"
				:disabled="searchLoading"
			/>
		</el-main>
		<infoteam
			:productDictList="productDictList"
			:title="title"
			:addComShow="addComShow"
			@comIsShow="comIsShow"
			@queryFundManagerList="queryFundManagerList"
			ref="infoteam"
		/>
	</div>
</template>
<script>
import { seachTeam, delTeam } from '@/api/productApi/assessment'
import infoteam from './components/info'
import { getFundList } from '@/api/productApi/mechanism'
import { downloadFile } from '@/api/productApi/file'
import { getEnum } from '@/api/productApi/file'
import { debounce } from '@/utils/debounceFunc'
export default {
	name: 'Team',
	components: {
		infoteam
	},
	data() {
		return {
			title: '',
			addComShow: false,
			fundManagerQuery: {
				fundFullName: '',
				invest: '',
				checkState: ''
			},
			fundManagerList: [],
			paging2: {
				// 显示的条目数
				size: 10,
				// 当前页数
				num: 1,
				total: 0,
				pages: 0,
				sizes: [10, 20, 50, 100]
			},
			opsList: ['f_invest_style_edit', 'f_assess_met_check', 'f_assess_met_del', 'f_invest_ratio_add'], // 操作权限
			fundList: [], // 基金列表（下拉框）
			checkList: [
				{ key: '', lable: '--全部--' },
				{ key: '0', lable: '待复核' },
				{ key: '1', lable: '已复核' },
				{ key: '2', lable: '已驳回' }
			],
			productDictList: [],
			searchLoading: false
		}
	},
	mounted() {
		this.queryFundManagerList()
		this.getFundList()
		this.getEnumList()
	},
	methods: {
		comIsShow() {
			this.addComShow = !this.addComShow
		},
		formCom(type, obj) {
			this.title = ''
			this.title = type
			this.comIsShow()
			if (obj) {
				this.$refs.infoteam.edit(JSON.stringify(obj))
			}
		},
		statuCheck(num) {
			this.paging2.num = num
			this.queryFundManagerList()
		},
		// 改变页数
		handleCurrentChange2: function(val) {
			this.paging2.num = val
			this.queryFundManagerList()
		},
		// 改变条数
		handleSizeChange2: function(val) {
			this.paging2.num = 1
			this.paging2.size = val
			this.queryFundManagerList()
		},
		// 基金-投资列表
		queryFundManagerList: debounce(function() {
			this.searchLoading = true
			seachTeam(this.fundManagerQuery, this.paging2)
				.then(res => {
					if (res.msgCode === 'Y') {
						res.data.body.forEach(item => {
							if (item.productFundInfo) {
								if (item.productFundInfo.baseInfo) {
									item.productFundInfo.fundFullName = item.productFundInfo.baseInfo.fundFullName
									item.productFundInfo.fundName = item.productFundInfo.baseInfo.fundName
								}
							}
							if (item.invest) {
								if (item.invest === '1') {
									item.nvestName = '固收非货基'
								} else if (item.invest === '2') {
									item.nvestName = '量化'
								} else if (item.invest === '3') {
									item.nvestName = 'ETF'
								} else if (item.invest === '4') {
									item.nvestName = '主动股票'
								} else if (item.invest === '5') {
									item.nvestName = '货基'
								}
							}
						})
						this.paging2.total = res.data.page.total
						this.fundManagerList = res.data.body
					}
				})
				.finally(() => {
					this.searchLoading = false
				})
		}, 10),
		getEnumList() {
			getEnum(['nvest']).then(res => {
				if (res.msgCode === 'Y') {
					this.productDictList = res.data.body.nvest
				}
			})
		},
		// 文件下载
		openFileDetails(obj) {
			downloadFile({ fid: obj.fileId }).then(item => {
				if (item.msgCode === 'N') {
					this.$message({ showClose: true, message: '文件不存在', type: 'error' })
					return
				}
				if (item) {
					this.$message({
						showClose: true,
						message: '正在下载文件，请稍等......',
						type: 'success'
					})
					window.open(`/update/download.do?fid=${obj.fileId}`)
				}
			})
		},
		// 基金列表（下拉框）
		getFundList() {
			getFundList({}).then(res => {
				if (res.msgCode === 'Y') {
					res.data.body.forEach(item => {
						if (item.baseInfo) {
							item.fundFullName = item.baseInfo.fundFullName
							item.fundName = item.baseInfo.fundName
							item.fundCode = item.baseInfo.fundCode
						}
					})
					this.fundList = res.data.body
				}
			})
		},
		delFundAssessment: function(item) {
			let fundCode = ''
			if (this.fundList.filter(items => items.fundSysCode === item.fundSysCode).length) {
				fundCode = this.fundList.filter(items => items.fundSysCode === item.fundSysCode)[0].baseInfo
					? this.fundList.filter(items => items.fundSysCode === item.fundSysCode)[0].baseInfo.fundCode
					: ''
			}
			this.operatorComfirm(() => {
				delTeam(item).then(res => {
					if (res.msgCode === 'Y') {
						if (this.paging2.total === 11) {
							this.paging2.num = 1
						}
						this.$message({ message: '删除成功', type: 'success' })
						this.queryFundManagerList()
					}
				})
			}, '确认是否删除该条基金[' + fundCode + '],' + '投资团队[' + item.nvestName + ']的数据')
		},
		tabFormatter(row, column, cellValue, index) {
			if (this.fundList.some(item => item.fundSysCode === cellValue)) {
				const obj = this.fundList.filter(item => item.fundSysCode === cellValue)
				return `${obj[0].baseInfo.fundCode}-${obj[0].baseInfo.fundFullName}`
			}
		},
		formatterCheckStatus(row, column, cellValue, index) {
			if (cellValue === '0') {
				return '待复核'
			} else if (cellValue === '1') {
				return '已复核'
			} else if (cellValue === '2') {
				return '已驳回'
			}
		}
	}
}
</script>
<style lang="scss" scoped src='./index.scss'>
</style>
